<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="utf-8">
    <title>代金券管理</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">
    <link rel="stylesheet" href="resources/layui/css/layui.css" media="all"/>
    <link rel="stylesheet" href="resources/css/public.css" media="all"/>
</head>
<body class="childrenBody">
<form class="layui-form">
    <blockquote class="layui-elem-quote quoteBox">
        <form class="layui-form">
            <div class="layui-inline">
                <div class="layui-input-inline">
                    序号： <input type="text" class="layui-input idVal" placeholder="请输入id"/>
                </div>
                <div class="layui-input-inline" >
                    代金券号： <input type="text" class="layui-input uuidVal" placeholder="请输入代金券号"/>
                </div>
                <div class="layui-input-inline">
                    金额： <input type="text" class="layui-input nominaVal" placeholder="请输入金额"/>
                </div>
                <a class="layui-btn search_btn" data-type="reload">搜索</a>
            </div>
            <div class="layui-inline">
                <a class="layui-btn layui-btn-danger layui-btn-normal addBtn">添加代金券</a>
            </div>
            <div class="layui-inline">
                <a class="layui-btn layui-btn-danger layui-btn-normal delAll_btn">批量删除</a>
            </div>
        </form>
    </blockquote>
    <table id="couponTable" lay-filter="couponTable"></table>
    <!--操作-->
    <script type="text/html" id="couponBar">
        <a class="layui-btn layui-btn-xs layui-btn-warm" lay-event="set">设置</a>
        <a class="layui-btn layui-btn-xs layui-btn-warm" lay-event="del">删除</a>
    </script>
</form>
<script type="text/javascript" src="resources/layui/layui.js"></script>
<script type="text/javascript" th:inline="none">
    layui.use(['form', 'layer', 'table', 'laytpl'], function () {
        var form = layui.form,
            layer = parent.layer === undefined ? layui.layer : top.layer,
            $ = layui.jquery,
            laytpl = layui.laytpl,
            table = layui.table;
        //用户列表
        var tableIns = table.render({
            elem: '#couponTable',
            url: 'TCSelectByPage.do',
            cellMinWidth: 95,
            page: true,
            height: "full-200",
            limits: [10, 15, 20, 25],
            limit: 10,
            id: "couponTablezz",
            cols: [[
                {type: "checkbox", fixed: "left", width: 50},
                {field: 'perCouponId', title: '序号', minWidth: 100, align: "center"},
                {field: 'perCouponUuid', title: '代金券号', minWidth: 100, align: "center"},
                {field: 'perCouponNominal', title: '金额', minWidth: 100, align: "center"},
                {
                    field: 'perCouponStarttime', title: '开始时间', align: 'center', minWidth: 90, templet: function (obj) {
                        return obj.perCouponStarttime;
                    }
                },
                {
                    field: 'perCouponEndtime', title: '失效时间', align: 'center', minWidth: 90, templet: function (obj) {
                        return obj.perCouponEndtime;
                    }
                },
                {field: 'perUserId', title: '用户', minWidth: 100, align: "center",templet:'#zz'},
                {field: 'perCouponStatus', title: '代金券状态', align: 'center', templet: function (obj) {
                        var res = '';
                       if (obj.perCouponStatus==0){
                           res= '未使用';
                       }else if (obj.perCouponStatus==1){
                           res= '已使用';
                       }else if (obj.perCouponStatus==2){
                           res= '已过期';
                       }
                       return res;
                    }},
                {title: '操作',  templet: '#couponBar', fixed: "right", align: "center"}
            ]]

        });

        //实现搜索功能
        $(".search_btn").click(function () {
            var perCouponId = $(".idVal").val();
            var perCouponUuid=$(".uuidVal").val();
            var perCouponNominal=$(".nominaVal").val();
            table.reload('couponTablezz', {
                where: {
                    perCouponId:perCouponId,
                    perCouponUuid:perCouponUuid,
                    perCouponNominal:perCouponNominal,
                }
            });
        });

        table.on("tool(couponTable)", function (obj) {
            // console.log(data.link_cust_no);
            //获取当前行的数据
            var data = obj.data;
            switch (obj.event) {
                case 'set':
                    setCoupon(data);
                    break;
                case 'del':
                    deleteCoupon(data);
                    break;
            }
        });

        //进入修改页面的弹出框
        function setCoupon(data) {
            var index = layui.layer.open({
                title: "设置代金券",
                type: 2,
                content: "toSetCoupon.do",
                area: ['650px', '600px'],
                success: function (layero, index) {
                    var body = layui.layer.getChildFrame('body', index);//少了这个是不能从父页面向子页面传值的
                    body.contents().find("#serviceIdHiden").val(data.perCouponId);//这样就将值传递给子页面中id为serviceIdHiden的隐藏域
                    body.contents().find(".perCouponEndtime").val(data.perCouponEndtime);
                    body.contents().find(".perCouponNominal").val(data.perCouponNominal);
                }
            });
            //layui.layer.full(index);//全屏显示
            window.sessionStorage.setItem("index", index);
            //改变窗口大小时，重置弹窗的宽高，防止超出可视区域（如F12调出debug的操作）
            $(window).on("resize", function () {
                layui.layer.full(window.sessionStorage.getItem("index"));
            })
        }

      function deleteCoupon(data) {
          layer.confirm('您确定要删除【' + data.perCouponUuid + '】这个代金券吗？', {icon: 3, title: '提示'}, function (index) {
              $.ajax({
                  url: "deleteCoupon.do",
                  data: {perCouponId: data.perCouponId},
                  type: "delete",
                  dataType: "json",
                  success: function (res) {
                      if (res.code == 200) {
                          //刷新表格
                          layer.msg(res.msg);
                          tableIns.reload();
                      } else if (res.code == -1) {
                          layer.msg(res.msg);
                      }
                  }
              });
              //关闭提示框
              layer.close(index);
          });
      }

        $(".delAll_btn").click(function () {
            var checkStatus = table.checkStatus('couponTablezz');
            var data = checkStatus.data;
            var uIds = [];
            if (data.length > 0) {
                for (var i in data) {
                    uIds.push(data[i].perCouponId);
                }
                layer.confirm('确定删除选中的代金券？', {icon: 3, title: '提示信息'}, function (index) {
                    $.ajax({
                        url: "deleteManyCoupon.do",
                        type: "delete",
                        data: {perCouponIds: uIds},
                        dataType: "json",
                        success: function (res) {
                            if (res.code == 200) {
                                layer.msg(res.msg);
                                tableIns.reload();
                            }
                        }
                    })
                    layer.close(index);
                })
            } else {
                layer.msg("请选择需要删除的用户");
            }
        });

        $(".addBtn").click(function () {
             var index=layui.layer.open({
                 title: "设置代金券",
                 type: 2,
                 content: "toAddCoupon.do",
                 area: ['800px', '800px'],
             })
            //layui.layer.full(index);//全屏显示
            window.sessionStorage.setItem("index", index);
            //改变窗口大小时，重置弹窗的宽高，防止超出可视区域（如F12调出debug的操作）
            $(window).on("resize", function () {
                layui.layer.full(window.sessionStorage.getItem("index"));
            })
        })


    });

</script>
<script id="zz" type="text/html">
    {{d.tperUser.perUserName}}
</script>
</body>
</html>